<template>
    <div class='edit-popup'>
        <popup
            ref='popupRef'
            :title='popupTitle'
            :async='true'
            width='660px'
            @confirm='handleSubmit'
            @close='handleClose'
        >
            <el-form ref='formRef' :model='formData' label-width='100px' :rules='formRules'>
                <el-form-item label='券类型' prop='quan_type'>
                    <el-radio-group v-model='formData.quan_type'>
                        <el-radio label='0'>满减券</el-radio>
                        <el-radio label='1'>商品兑换券</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label='优惠券名称' prop='title'>
                    <el-input v-model='formData.title' clearable placeholder='请输入优惠券名称' />
                </el-form-item>
                <el-form-item label='优惠金额' prop='coupon'>
                    <el-input v-model='formData.coupon' clearable placeholder='请输入优惠金额' />
                </el-form-item>
                <el-form-item label='满减金额' prop='full_deduction'>
                    <el-input v-model='formData.full_deduction' clearable placeholder='请输入满减金额' />
                </el-form-item>
                <el-form-item label='有效期' prop='indate'>
                    <el-date-picker placeholder='请选择有效期' type='datetime' value-format='YYYY-MM-DD HH:mm:ss' v-model='formData.indate' />
<!--                    <el-input v-model='formData.indate' clearable placeholder='请输入有效期' />-->
                </el-form-item>
                <el-form-item label='优惠券数量' prop='count'>
                    <el-input v-model='formData.count' clearable placeholder='请输入优惠券数量' />
                </el-form-item>
                <el-form-item label='是否限定数量' prop='type'>
                    <el-radio-group v-model='formData.type'>
                        <el-radio label='1'>限定数量</el-radio>
                        <el-radio label='2'>不限定数量</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang='ts' setup name='discountCouponEdit'>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiDiscountCouponAdd, apiDiscountCouponEdit, apiDiscountCouponDetail } from '@/api/discount_coupon'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑优惠券管理' : '新增优惠券管理'
})

// 表单数据
const formData = reactive({
    id: '',
    title: '',
    coupon: '',
    full_deduction: '',
    indate: '',
    count: '',
    type: '1',
    quan_type:'0'
})


// 表单验证
const formRules = reactive<any>({})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    console.log(data,'data')
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiDiscountCouponDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await apiDiscountCouponEdit(data)
        : await apiDiscountCouponAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
